package cn.zyq.ykb.service.task.impl;

import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.task.TaskMaterialMapper;
import cn.zyq.ykb.beans.task.TaskMaterial;
import cn.zyq.ykb.service.task.TaskMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 任务-做单素材Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-05-22
 */
@Service
public class TaskMaterialServiceImpl  extends BaseService implements TaskMaterialService {

    @Autowired
    private TaskMaterialMapper taskMaterialMapper;

    /**
     * 查询任务-做单素材
     *
     * @param taskMaterialID 任务-做单素材主键
     * @return 任务-做单素材
     */
    @Override
    public TaskMaterial selectTaskMaterialByTaskMaterialID(Integer taskMaterialID) {
        TaskMaterial taskMaterial = taskMaterialMapper.selectByPrimaryKey(taskMaterialID);
        if (taskMaterial.getDeleted() == 1) {
            return null;
        }
        return taskMaterial;
    }

    /**
     * 查询任务-做单素材
     *
     * @param serviceID 业务ID
     * @return 任务-做单素材
     */
    @Override
    public TaskMaterial selectTaskMaterialByServiceID(String serviceID) {
        TaskMaterial param = new TaskMaterial();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.taskMaterialMapper.selectOne(param);
    }

    /**
     * 查询任务-做单素材列表
     *
     * @param pageParam
     * @return 任务-做单素材
     */
    @Override
    public PageInfo<TaskMaterial> selectTaskMaterialList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<TaskMaterial> list = this.taskMaterialMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询任务-做单素材列表
     *
     * @param param
     * @return 任务-做单素材
     */
    @Override
    public List<TaskMaterial> selectTaskMaterialList(TaskMaterial param) {
        param.setDeleted(0);
        return this.taskMaterialMapper.select(param);
    }
    /**
     * 新增任务-做单素材
     *
     * @param param 任务-做单素材
     * @return 结果
     */
    @Override
    public int insertTaskMaterial(TaskMaterial param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.taskMaterialMapper.insertSelective(param);
    }

    /**
     * 修改任务-做单素材
     *
     * @param param 任务-做单素材
     * @return 结果
     */
    @Override
    public int updateTaskMaterial(TaskMaterial param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.taskMaterialMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除任务-做单素材
     *
     * @param taskMaterialIDs 需要删除的任务-做单素材主键
     * @return 结果
     */
    @Override
    public int deleteTaskMaterialByTaskMaterialIDs(Integer[] taskMaterialIDs) {
        int i = 0;
        for (Integer taskMaterialID : taskMaterialIDs) {
            i += this.deleteTaskMaterialByTaskMaterialID(taskMaterialID);
        }
        return i;
    }

    /**
     * 删除任务-做单素材信息
     *
     * @param taskMaterialID 任务-做单素材主键
     * @return 结果
     */
    @Override
    public int deleteTaskMaterialByTaskMaterialID(Integer taskMaterialID) {
        TaskMaterial taskMaterial = this.selectTaskMaterialByTaskMaterialID(taskMaterialID);
        if (taskMaterial == null) {
            return 0;
        } else {
            TaskMaterial storeInfo = new TaskMaterial();
            storeInfo.setTaskMaterialID(taskMaterialID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.taskMaterialMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
